//
//  ThreeListViewController.m
//  RedManStock
//
//  Created by lanou3g on 15/7/21.
//  Copyright (c) 2015年 lanou3g. All rights reserved.
//

#import "ThreeListViewController.h"
#import "ThreeListTableViewCell.h"
#import "ThreeRaceMan.h"
#import "MBProgressHUD.h"
@interface ThreeListViewController ()

@property (nonatomic,retain) NSMutableArray *threeRaceManArray;

@end

@implementation ThreeListViewController

//三个榜单:  总收益榜 胜率榜 人气榜   race/list
//参数: id=0,1,2  分别对应上面三个榜单的参数
//
//http://api.gongniucaijing.com/race/list?id=0&idfa=7B7C9B42-5C18-4EEB-A0F8-6708DE081AAE&machine=iPhone%205&number=20&offset=0&os=iPhone%20OS%208.2&platform=1&protocolVersion=1.0&qudao=0000001&resolution=640%2A1136&token=d58f02522dac7e698a4600c8b18ff02c0b9fa6df&uuid=BULLSTOCK-79460017-C3E5-45EC-B98C-1DD541A61127&version=1.9.0&versionCode=190

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.threeRaceManArray=[NSMutableArray array];
    
    [self.tableView registerNib:[UINib nibWithNibName:@"ThreeListTableViewCell" bundle:nil] forCellReuseIdentifier:@"ThreeListTableViewCell"];
    
    [self loadNewData];
    
}

- (void)loadNewData{
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    
    NSString *url=@"http://api.gongniucaijing.com/race/list";
    
    [[MyHandle new] requestDataWithURLStr:url parameterBlock:^NSDictionary *{
        
        NSDictionary *dic=@{@"id":[NSString stringWithFormat:@"%lu",self.requestId],
                            @"offset":@"0",
                            @"number":@"20"};
        return dic;
    } successBlock:^(NSDictionary *responseDic) {
        
         NSMutableArray *tempArray=[NSMutableArray array];
         NSArray *array=[[responseDic objectForKey:@"data"] objectForKey:@"lists"];
         for (NSDictionary *dic in array) {
             ThreeRaceMan *threeRaceMan=[[ThreeRaceMan alloc]init];
             threeRaceMan.uniqueId=self.requestId;
             threeRaceMan.ranking=[[dic objectForKey:@"ranking"] stringValue];
             threeRaceMan.name=[dic objectForKey:@"name"];
             threeRaceMan.avatarStr=[dic objectForKey:@"avatar"];
             if (self.requestId==0) {
                 float a=[[dic objectForKey:@"raceYieldRate"] floatValue]*100;
                 threeRaceMan.raceYieldRate=[NSString stringWithFormat:@"%.2f%%",a];
             }
             else if(self.requestId==1){
                 threeRaceMan.winning=[[dic objectForKey:@"winning"] stringValue];
             }
             else{
                 threeRaceMan.fans=[[dic objectForKey:@"fans"] stringValue];
             }
             [tempArray addObject:threeRaceMan];
         }
        self.threeRaceManArray=tempArray;
        [self.tableView reloadData];
        [self.tableView.header endRefreshing];
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        
    } errorBlock:^(NSError *error) {
        [self setUpMBProgressWithText:@"网络错误，请检查网络"];
    }];
    
}

- (void)setUpMBProgressWithText:(NSString *)text{
    MBProgressHUD *hud=[MBProgressHUD showHUDAddedTo:self.view animated:YES];
    hud.mode=MBProgressHUDModeText;
    hud.labelText=text;
    [hud hide:YES afterDelay:1.5f];
}

- (void)loadMoreData{
    
    NSString *url=@"http://api.gongniucaijing.com/race/list";
    
    [[MyHandle new] requestDataWithURLStr:url parameterBlock:^NSDictionary *{
        
        NSDictionary *dic=@{@"id":[NSString stringWithFormat:@"%lu",(long)self.requestId],
                            @"offset":[NSString stringWithFormat:@"%lu",(unsigned long)self.threeRaceManArray.count],
                            @"number":@"20"};
        return dic;
    } successBlock:^(NSDictionary *responseDic) {
        
        NSMutableArray *tempArray=[NSMutableArray array];
        NSArray *array=[[responseDic objectForKey:@"data"] objectForKey:@"lists"];
        for (NSDictionary *dic in array) {
            ThreeRaceMan *threeRaceMan=[[ThreeRaceMan alloc]init];
            threeRaceMan.uniqueId=self.requestId;
            threeRaceMan.ranking=[[dic objectForKey:@"ranking"] stringValue];
            threeRaceMan.name=[dic objectForKey:@"name"];
            threeRaceMan.avatarStr=[dic objectForKey:@"avatar"];
            if (self.requestId==0) {
                float a=[[dic objectForKey:@"raceYieldRate"] floatValue]*100;
                threeRaceMan.raceYieldRate=[NSString stringWithFormat:@"%.2f%%",a];
            }
            else if(self.requestId==1){
                threeRaceMan.winning=[[dic objectForKey:@"winning"] stringValue];
            }
            else{
                threeRaceMan.fans=[[dic objectForKey:@"fans"] stringValue];
            }
            [tempArray addObject:threeRaceMan];
        }
        [self.threeRaceManArray addObjectsFromArray:tempArray];
        [self.tableView reloadData];
        [self.tableView.footer endRefreshing];
        
    } errorBlock:^(NSError *error) {
        NSLog(@"error=%@",error);
        
    }];
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 70;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.threeRaceManArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    ThreeListTableViewCell *cell=[self.tableView dequeueReusableCellWithIdentifier:@"ThreeListTableViewCell" forIndexPath:indexPath];
    
    ThreeRaceMan *threeMan=self.threeRaceManArray[indexPath.row];
    cell.threeRaceMan=threeMan;
    
    if (threeMan.avatarImage==nil) {
        
        [cell.avatorImageView sd_setImageWithURL:[NSURL URLWithString:threeMan.avatarStr] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
            threeMan.avatarImage=image;
        }];
    }
    cell.avatorImageView.image=threeMan.avatarImage;
    return cell;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
